function Player(parent, ctrlParent, starterVideo) {
    this.parent = parent;
    this.ctrlParent = ctrlParent;
    this.starterVideo = starterVideo;
    this.create();
    this.isPlaying = false;
    this.loop = true;
    this.aspectRatio = 1.77;
    this.videoId = "";
    if ("undefined" != typeof starterVideo){
        this.changeSrc(starterVideo, true);
    }
};

Player.prototype.create = function () {
    this.parent.html('<iframe id="UstreamIframe" src="http://www.ustream.tv/embed/20137366?controls=false&amp;autoplay=true" width="100%" height="100%"></iframe>');
    /*
    this.player.on("emptied", $.proxy(this.handleVideoEmptied, this));
    this.player.on("timeupdate", $.proxy(this.handleVideoTimeUpdate, this));
    */
    this.ctrl = $("<span id=\"video_ctrl\">&#9612 &#9612</span>").click($.proxy(this.togglePlayPause, this));
    this.ctrlParent.append(this.ctrl);
    this.viewer = UstreamEmbed('UstreamIframe');
    this.viewer.addListener('playing', $.proxy(function(e, b) {
        this.isPlaying = b; 
        if (this.isPlaying) {
        	this.ctrl.html("&#9612;&#9612;");
        	this.handleVideoPlaying();
        }
        else
        	this.ctrl.html("&#9658;");
    }, this));
    this.viewer.addListener('finished', $.proxy(this.handleVideoEnded, this));
    this.viewer.addListener('size', $.proxy(this.handleVideoSize, this));
    
    setInterval(this.getProgress, 1000);

};

Player.prototype.togglePlayPause = function () {
    if (this.isPlaying) {
        this.pause();
        this.ctrl.html("&#9658;");
    }
    else {
        this.play();
        this.ctrl.html("&#9612;&#9612;");
    }
};

Player.prototype.getTitleVideo = function () {
    return this.starterVideo;
};

Player.prototype.changeSrc = function (videoSrc, loop, videoPosition) {
    if ("undefined" != typeof loop && loop == true) {
        this.loop = true;
    }
    else {
        this.loop = false;
    }
    if (this.videoId != videoSrc) {
    	$("#loaderAnim").addClass("active");
        $("#player").addClass("idle");
        this.videoId = videoSrc;
        this.viewer.callMethod('load', 'video', videoSrc);
        this.startingPosition = videoPosition ? videoPosition : 0;
    }
    else
    {
        this.viewer.callMethod("seek", videoPosition ? videoPosition : 0);
    }
};

Player.prototype.play = function () {
	this.isPlaying = true;
    this.viewer.callMethod('play');
    
};

Player.prototype.pause = function () {
    this.viewer.callMethod('pause');
    this.isPlaying = false;
};

Player.prototype.stop = function () {
    this.viewer.callMethod('stop');
    this.isPlaying = false;
};

Player.prototype.easeIn = function (done) {
	//disabled by csabi
	return;
    var thiz = this;
    var smoothness = 500; // ms
    var state = 0;
    var easeInFunction = function (t) { return t; }
    var f = function() {
        thiz.play();
        thiz.player.get()[0].volume = Number(easeInFunction(state).toFixed(2));
        state += 0.1;
        if (state <= 1) {
            setTimeout(f, smoothness);
        }
        else {
            if ("undefined" != typeof done) {
                done();
            }
        }
    };
    setTimeout(f, smoothness);
};

Player.prototype.easeOut = function (done) {
	//disabled by csabi
	return;
    var thiz = this;
    var smoothness = 50; // ms
    var state = 1;
    var easeOutFunction = function (t) { return t; }
    var f = function() {
        thiz.player.get()[0].volume = Number(easeOutFunction(state).toFixed(2));
        state -= 0.1;
        if (state >= 0) {
            setTimeout(f, smoothness);
        }
        else {
            thiz.stop();
            if ("undefined" != typeof done) {
                done();
            }
        }
    };
    setTimeout(f, smoothness);
};

Player.prototype.addVideoEndHandler = function (handler) {
    this.videoEndedHandler = handler;
};

Player.prototype.handleVideoEnded = function () {
	if (this.loop)
		this.play();
    else {
    	this.isPlaying = false;
    	if ("undefined" != typeof this.videoEndedHandler) {
        	this.videoEndedHandler();
    	}
    	
    }
};

Player.prototype.addVideoTimeUpdateHandler = function (handler) {
    this.videoTimeUpdateHandler = handler;
};

Player.prototype.handleVideoProgress = function (e) {
	if (this.isPlaying) {
	    if (e > 1) {
		    if ("undefined" != typeof this.videoTimeUpdateHandler) {
		        this.videoTimeUpdateHandler(e);
		    }
		    $("#player").removeClass("idle");
		    $("#loaderAnim").removeClass("active")
		    if (this.startingPosition && e > 0) {
		    	this.viewer.callMethod('seek', this.startingPosition);
    			this.startingPosition = null;
    		}
		}
	    
	}

};

Player.prototype.getProgress = function ()
{
	Player.viewer.getProperty('progress', $.proxy(Player.handleVideoProgress, Player));
}


Player.prototype.handleVideoSize = function (e, p) {
	this.aspectRatio = p[0] / p[1];
	this.resizeVideo();
};

Player.prototype.resizeVideo = function () {
    var w = $(window).width();
    var h = $(window).height();
    var r = w / h;
    if (r > this.aspectRatio) {
    	$("#player").css("width", "100%");
    	$("#player").css("height", Math.round(w / this.aspectRatio) + "px");
    }
    else {
    	$("#player").css("width", Math.round(h * this.aspectRatio) + "px");
    	$("#player").css("height", "100%");
    }
    //$("#player").css("height", p[1] + 1 + "px");
};


Player.prototype.handleVideoWaiting = function () {
    
    $("#loaderAnim").addClass("active");
};

Player.prototype.handleVideoPlaying = function () {
    
    
};

Player.prototype.handleVideoEmptied = function () {
    //console.log("Video emptied");
};